import { request, config, storage } from 'utils';

const { api } = config;
const { nodeSave, getReportListData, getAllUserDashboard, setDashboardMenuShow } = api;

// 将 用户信息存在 node端
export async function saveUserInfo(params = {}) {
  return request(nodeSave, {
    data: {
      ...params,
    },
  });
}

// 获取 该用户下所有仪表盘
export async function GetAllUserDashboard() {
  const UID = storage.getItem('UID');
  if (!UID) {
    console.warn('UID 丢失!');
    return [];
  }
  const body = {
    queryDTO: {
      UID,
      Dashboard: { Name: '', IsEnabled: 1, UID },
    },
  };
  const { data } = await request(getAllUserDashboard, { data: body });
  return data || [];
}

// 通过报表列表IDs 获取 报表列表
export async function GetReportListData(IDs = []) {
  const body = {
    ds: { IDs },
    sortPredicate: '',
    sortOrder: 1,
  };
  const { data } = await request(getReportListData, { data: body, loading: false });
  return data || [];
}

// 设为菜单
export async function SetDashboardMenuShow(data) {
  if (!data) return {};
  const UID = storage.getItem('UID');

  // 判断是批量设置菜单还是单个， 都改成批量
  const mnList = data instanceof Array ? data : [data];

  // 批量发起请求
  const asyncList = mnList.map(item => {
    const { DID, IsMenuShow } = item;
    const body = { DID, IsMenuShow, UID };
    return request(setDashboardMenuShow, { data: body });
  });
  const resList = await Promise.all(asyncList);
  return resList;
}
